package plate

//判断是否有车无车,或者离线 0:无车,1:有车,2:离线
func judgeWhetherCar(last CarResult_t, carResults []CarResult_t) (ret int) {
	isEmpytCount := 0
	for _, value := range carResults {
		if value.IsEmpty {
			isEmpytCount++
		}
	}
	if isEmpytCount == len(carResults) {
		ret = 2
		return
	}
	var (
		whetherYes float32
		whetherNo  float32
		temp       float32
	)
	//temp = whetherNo + whetherYes
	if last.Car.CarType == "" {
		if len(carResults) >= 5 {
			whetherNo = 0
			whetherYes = 0
			for _, value := range carResults[len(carResults)-5:] {
				if value.Car.CarType == "" {
					whetherNo++
				} else {
					whetherYes++
				}
			}
			temp = whetherNo + whetherYes
		} else {
			whetherNo = 0
			whetherYes = 0
			for _, value := range carResults {
				if value.Car.CarType == "" {
					whetherNo++
				} else {
					whetherYes++
				}
			}
			temp = whetherNo + whetherYes
		}
		//无车>其他状态
		if whetherYes > (temp * 0.6) {
			ret = 1
		} else {
			ret = 0
		}
	} else {
		if len(carResults) >= 5 {
			whetherNo = 0
			whetherYes = 0
			for _, value := range carResults[len(carResults)-5:] {
				if value.Car.CarType == "" {
					whetherNo++
				} else {
					whetherYes++
				}
			}
			if whetherYes == 0 {
				ret = 0
			} else {
				ret = 1
			}
		} else {
			whetherNo = 0
			whetherYes = 0
			for _, value := range carResults {
				if value.Car.CarType == "" {
					whetherNo++
				} else {
					whetherYes++
				}
			}
		}
		if whetherYes == 0 {
			ret = 0
		} else {
			ret = 1
		}
	}

	return
}

//根据历史记录,以及队列的数据,得到数据
func judgeGetCarResult(last CarResult_t, results []CarResult_t, whether int) (CarType, PlateNO, PlateColor string) {
	var (
		temp            float32
		maxPlateNo      int
		tempByte        byte
		tempByteArray   []byte
		tempStr         string
		firstIsHanCount int
		theFirst        string
		realCount       float32
	)
	carTypes := make(map[string]float32)
	plateColors := make(map[string]float32)
	plateNOs := make(map[string]float32)
	theFirstPlateNOs := make(map[string]float32)
	for _, value := range results {
		if value.Car.CarType == "" {
			continue
		}
		realCount++
		//车辆类型统计
		carType, ok := carTypes[value.Car.CarType]
		if ok {
			carTypes[value.Car.CarType] = carType + 1
		} else {
			carTypes[value.Car.CarType] = 1
		}
		//颜色统计
		plateColor, ok := plateColors[value.Car.PlateColor]
		if ok {
			plateColors[value.Car.PlateColor] = plateColor + 1
		} else {
			plateColors[value.Car.PlateColor] = 1
		}

		//车牌号
		plateNO, ok := plateNOs[value.Car.PlateNO]
		if ok {
			plateNOs[value.Car.PlateNO] = plateNO + 1
		} else {
			plateNOs[value.Car.PlateNO] = 1
		}

		//判断首位是否是汉字,并作出对比
		if theFirstIsHanDe(value.Car.PlateNO) {
			firstIsHanCount++
			plateNO, ok := theFirstPlateNOs[value.Car.PlateNO[0:3]]
			if ok {
				theFirstPlateNOs[value.Car.PlateNO[0:3]] = plateNO + 1
			} else {
				theFirstPlateNOs[value.Car.PlateNO[0:3]] = 1
			}
		}

		if len(value.Car.PlateNO) > maxPlateNo {
			maxPlateNo = len(value.Car.PlateNO)
		}

	}

	//确定车辆类型
	temp = 0
	for key := range carTypes {
		if carTypes[key] > temp {
			CarType = key
			temp = carTypes[key]
		}
	}

	//确定车牌颜色
	temp = 0
	for key := range plateColors {
		if plateColors[key] > temp {
			if key == "" {
				continue
			}
			PlateColor = key
			temp = plateColors[key]
		}
	}
	if last.Car.PlateColor == "" {

	} else if last.Car.PlateColor != PlateColor {
		if temp < realCount*0.9 {
			PlateColor = last.Car.PlateColor
		}
	}

	//add by xqm 新增历史记录对比权重
	if theFirstIsHanDe(last.Car.PlateNO) {
		plateNO, ok := theFirstPlateNOs[last.Car.PlateNO[0:3]]
		if ok {
			theFirstPlateNOs[last.Car.PlateNO[0:3]] = plateNO + 2.5
		} else {
			theFirstPlateNOs[last.Car.PlateNO[0:3]] = 2.5
		}
	}
	//endif 20220606

	//车牌号
	PlateNO = ""
	if firstIsHanCount*2 >= len(results) {
		//第一位是汉字,确认第一位数
		temp = 0
		for key := range theFirstPlateNOs {
			if theFirstPlateNOs[key] > temp {
				theFirst = key
				temp = theFirstPlateNOs[key]
			}
		}

		PlateNO = theFirst
		if maxPlateNo > 3 {
			for i := 3; i < maxPlateNo; i++ {
				oneNOs := make(map[byte]float32)
				for key, value := range results {
					if theFirstIsHanDe(value.Car.PlateNO) && len(value.Car.PlateNO) > i {
						//车牌号
						oneNO, ok := oneNOs[value.Car.PlateNO[i]]
						if ok {
							oneNOs[value.Car.PlateNO[i]] = oneNO + 1
						} else {
							oneNOs[value.Car.PlateNO[i]] = 1
						}
					}
					if key+1 == len(results) {
						//add by xqm
						if theFirstIsHanDe(last.Car.PlateNO) && len(last.Car.PlateNO) > i {
							//车牌号
							oneNO, ok := oneNOs[last.Car.PlateNO[i]]
							if ok {
								oneNOs[last.Car.PlateNO[i]] = oneNO + 2.5
							} else {
								oneNOs[last.Car.PlateNO[i]] = 2.5
							}
						}
						//end 20220606
						temp = 0
						tempByte = 0
						for key := range oneNOs {
							if oneNOs[key] > temp {
								tempByte = key
								temp = oneNOs[key]
							}
						}
						tempByteArray = append(tempByteArray, tempByte)
					}
				}
			}
			PlateNO = PlateNO + string(tempByteArray)
		}

	} else {
		//第一位不是汉字,按字节
		for i := 0; i < maxPlateNo; i++ {
			oneNOs := make(map[byte]float32)
			for key, value := range results {
				if len(value.Car.PlateNO) > i {
					//车牌号
					oneNO, ok := oneNOs[value.Car.PlateNO[i]]
					if ok {
						oneNOs[value.Car.PlateNO[i]] = oneNO + 1
					} else {
						oneNOs[value.Car.PlateNO[i]] = 1
					}
				}
				if key+1 == len(results) {
					//add by xqm
					if len(last.Car.PlateNO) > i {
						//车牌号
						oneNO, ok := oneNOs[last.Car.PlateNO[i]]
						if ok {
							oneNOs[last.Car.PlateNO[i]] = oneNO + 2.5
						} else {
							oneNOs[last.Car.PlateNO[i]] = 2.5
						}
					}
					//end 20220606
					temp = 0
					tempByte = 0
					for key := range oneNOs {
						if oneNOs[key] > temp {
							tempByte = key
							temp = oneNOs[key]
						}
					}
					tempByteArray = append(tempByteArray, tempByte)
				}
			}
		}
		PlateNO = string(tempByteArray)
	}

	//统计车牌最多出现的次数
	temp = 0
	for key := range plateNOs {
		if plateNOs[key] > temp {
			if key == "" {
				continue
			}
			tempStr = key
			temp = plateNOs[key]
		}
	}

	if last.Car.PlateNO == "" {
		//上一个值为空,立即给值
		if temp < realCount*0.3 {
			PlateNO = last.Car.PlateNO
		}
	} else {
		//计算结果和投票结果不一致
		if plateNoLenIsOk(tempStr, PlateColor) {
			// if tempStr != PlateNO {
			// 	if temp > realCount*0.85 {
			// 		//PlateNO = tempStr

			// 	} else {
			// 		PlateNO = last.Car.PlateNO
			// 	}
			// } else {
			// 	if temp > realCount*0.75 {
			// 		//PlateNO = tempStr
			// 	} else {
			// 		PlateNO = last.Car.PlateNO
			// 	}
			// }
			if temp > realCount*0.85 {
				//PlateNO = tempStr
			} else {
				PlateNO = last.Car.PlateNO
			}
		} else {
			PlateNO = last.Car.PlateNO
		}
	}

	if PlateNO == "" && PlateColor != "" {
		PlateColor = ""
	}

	//for i := 0; i < maxPlateNo; i++ {

	//}
	/*
		for _, value := range results {
			if value.isEmpty == true || value.Car.CarType == "" {
				continue
			}
		}
	*/
	return
}
